package sf.database.template;

import org.smallframework.spring.path.ClassPathScanningUtils;
import sf.database.OrmConfig;
import sf.database.template.enjoy.EnjoyHandler;
import sf.database.template.freemarker.FreemarkerHandler;
import sf.database.template.mybatis.MybatisHandler;
import sf.tools.StringUtils;

import java.net.URL;
import java.util.Set;

public class TemplateRender {
    public static TemplateHandler getTemplateHandler(TemplateType type) {
        if (type == null) {
            type = OrmConfig.getInstance().getSqlTemplateType();//默认使用enjoy
        }
        switch (type) {
            case enjoy:
                return EnjoyHandler.getInstance();
            case freemarker:
                return FreemarkerHandler.getInstance();
            case mybatis:
                return MybatisHandler.getInstance();
            default:
                return EnjoyHandler.getInstance();
        }
    }

    public static String[] defaultPath = new String[]{"classpath*:sql/**/*.sql"};

    public static Set<URL> getDefaultSqlTemplates() {
        return getSqlTemplates(defaultPath);
    }

    public static Set<URL> getSqlTemplates(String[] paths) {
        return ClassPathScanningUtils.getPathScanning().getResourcesURL(null, paths);
    }

    /**
     * 跨数据库的key,如果dbType为空,
     * @param key    sqlId
     * @param dbType 数据库类型
     * @return
     */
    public static String wrapKey(String key, String dbType) {
        if (StringUtils.isNotBlank(dbType)) {
            return key + "." + dbType;
        } else {
            return key;
        }
    }
}
